﻿using UnityEngine;
using System.Collections;

namespace Game
{
    /// <summary>
    /// 用于监听Trigger的碰撞事件的组件
    /// <para>1)可用于脚本添加，以接收碰撞消息</para>
    /// </summary>
	public class GameObjTrigger : MonoBehaviour
    {

#if UNITY_EDITOR
        void Start () 
		{
		    //CHECK:
            if (this.GetComponent<Collider>() == null)
            {
                Debug.LogError("NO COLLIDER FOUND... Component will be destroyed...");
                Debug.LogError("Please attach a <collider> to obj before using this component.");
                
                Destroy(this);
                return;
            }
            else if (this.GetComponents<GameObjTrigger>().Length > 1)
            {
                Debug.LogWarning("Multiple components 'GameObjTrigger' added to " + name);
            }

		}
#endif
        /// <summary>
        /// Trigger的控制类
        /// </summary>
        /// <remarks>在相互碰撞时，可通过此找到碰撞体来源</remarks>
        object m_triggerController;

        /// <summary>
        /// 在碰撞时触发函数处理
        /// </summary>
        CallbackFuncWithCollider m_cbOnGOEnterTrigger;


        /// <summary>
        /// 在碰撞退出时触发函数处理
        /// </summary>
        CallbackFuncWithCollider m_cbOnGOExitTrigger;

        #region 公用函数：设置回调

        [System.Obsolete("Obsolete, use Init instead.")]
        /// <summary>
        /// Init 设置Trigger各参数
        /// </summary>
        /// <param name="controlClass">控制此Trigger的类</param>
        /// <param name="onGameObjEnter">开始碰撞时回调</param>
        /// <param name="onGameObjExit">退出碰撞时回调</param>
        public void Create(object controlClass ,CallbackFuncWithCollider onGameObjEnter, CallbackFuncWithCollider onGameObjExit)
        {
            Init(controlClass ,onGameObjEnter, onGameObjExit);
        }

        /// <summary>
        /// 设置Trigger各参数
        /// </summary>
        /// <param name="controlClass">控制此Trigger的类</param>
        /// <param name="onGameObjEnter">开始碰撞时回调</param>
        /// <param name="onGameObjExit">退出碰撞时回调</param>
        public void Init(object controlClass, CallbackFuncWithCollider onGameObjEnter,  CallbackFuncWithCollider onGameObjExit)
        {
            //m_triggerController = controlClass;

            SetOnTriggerEnterCallback(onGameObjEnter);
            SetOnTriggerExitCallback(onGameObjExit);
        }


        /// <summary>
        /// 设置碰撞体与其他碰撞体碰撞时回调事件
        /// </summary>
        public void SetOnTriggerEnterCallback(CallbackFuncWithCollider onGameObjEnter)
        {
            m_cbOnGOEnterTrigger = onGameObjEnter;
        }

        /// <summary>
        /// 设置碰撞体与其他碰撞体退出碰撞时回调事件
        /// </summary>
        public void SetOnTriggerExitCallback(CallbackFuncWithCollider onGameObjExit)
        {
            m_cbOnGOExitTrigger = onGameObjExit;
        }

        ///// <summary>
        ///// 获得对应的控制类实例，无则null
        ///// </summary>
        ///// <typeparam name="T">类的类型</typeparam>
        //public T TryGetController<T>() 
        //{
        //    //--类型对比，是所需要的类，则将返回控制的类的实例
        //    if (typeof(T) != m_triggerController.GetType())
        //    {
        //        return default(T);//无则null
        //    }
        //    else
        //    {
        //        return (T)m_triggerController;
        //    }
        //}

        #endregion

        #region Mono Trigger 事件
        void OnTriggerEnter(Collider col)
        {
            if (m_cbOnGOEnterTrigger != null)
            {
                //调用该回调：
                m_cbOnGOEnterTrigger(col);
            }
        }

        void OnTriggerExit(Collider col)
        {
            if (m_cbOnGOExitTrigger != null)
            {
                m_cbOnGOExitTrigger(col);
            }
        }
        #endregion

    }

}